<?php


namespace backend\controllers;

use backend\models\NewsType;
use yii;
use backend\models\News;
use backend\models\UsersTag;

class HelloController extends \yii\web\Controller
{
    public $layout = 'common';

    public function behaviors()
    {
//        return [
//            [
//                'class' => 'yii\filters\PageCache',
//                'only' => ['index'],
//                'duration' => 60,
//                'variations' => [
//                    \Yii::$app->language,
//                ],
//                'dependency' => [
//                    'class' => 'yii\caching\DbDependency',
//                    'sql' => 'SELECT COUNT(*) FROM user',
//                ],
//            ],
//        ];
        return [
            [
                'class' => 'yii\filters\HttpCache',
                'only' => ['index'],
                'lastModified' => function ($action, $params) {
                    $q = new \yii\db\Query();
                    return $q->from('user')->max('updated_at');
                },
                'etagSeed' => function ($action, $params){
                    return 'etag_string';
                }
            ],
        ];
    }

    public function actionIndex()
    {
        $request = YII::$app->request;
        if ($request->isGet) {
            echo 'this is a get request!';
        }
        echo 'userip:' . $request->userIP;
        echo 'userhost:' . $request->userHost;
        $id = $request->get('id', 1);
        echo 'id:' . $id;
        echo '<br/>';
        echo 'hello world!!';
        return $this->render('index', ['name' => 'zhangsan', 'sex' => 'man']); //$content
    }

    public function actionResponse()
    {
        $response = YII::$app->response;
        $response->statusCode = 200;
        $response->headers->add('Pragma', 'no-cache');
//            $response->headers->add('location', 'https://baidu.com');
//            $this->redirect('https://baidu.com');
        $response->sendFile('./robots.txt')->send();
    }

    public function actionSession()
    {
//        $_SESSION['user'] ='zhangsan';
//        echo $_SESSION['user'];
        // 开启session
        $session = Yii::$app->session;
        $session->open();
        if ($session->isActive) {
            echo 'session开启';
            $session->set('language', 'en-US');
            if ($session->has('language')) {
                echo $session->get('language');
            }

//            $session->remove('language');

        }
    }

    public function actionCookie()
    {
        //获取cookies
        $recookies = Yii::$app->request->cookies;
        echo $language = $recookies->getValue('advanced-backend', 'en');

        $cookies = Yii::$app->response->cookies;
        // 在要发送的响应中添加一个新的 cookie
        $cookies->add(new \yii\web\Cookie([
            'name' => 'language',
            'value' => 'zh-CN',
        ]));
        echo $language = $recookies->getValue('language', 'en');
        $cookies->remove('language');
    }

    public function actionDb()
    {
        $sql = "select * from news where id=1";
        $list = News::findBySql($sql)->all();
        $list = News::find()->where(['>', 'id', 0])->asArray()->all();
        var_dump($list);

        $object = new News;
//        $object->id = 2;
        $object->title = '好今天';
        $object->content = '星期天是个好日子，出去爬山去了！';
        $object->create_time = time();
        $object->validate();
        if ($object->hasErrors()) {
            echo 'save data error!';
            exit();
        }
        $object->save();
    }

    public function actionDelete()
    {
//        $NEWS = new News;
//        $NEWS::deleteAll('id=2');
//        $one = News::findOne("3");
        $one = News::find()->where(['id'=>1])->one();
        $one->title = '今天是星期天';
        echo $one->update();
    }
    public function actionJoinquery()
    {
//        $one = News::find()->where(['id'=>1])->one();
//        $nesone = $one->hasOne(NewsType::className(), ['id' => 'type'])->asArray()->one();
//        var_dump($nesone);
//        $one = News::find()->where(['id'=>1])->one();
//        $nesone = $one->newstype;
//        var_dump($nesone);
        $all = News::find()->with('newstype')->all();
        foreach ($all as $v){
            $nesone = $v->newstype;
            var_dump($nesone);
        }
    }
    public function actionCache()
    {
        $cache = Yii::$app->cache;
//        $cache->add('name','lisi');
//        $cache->set('name','hello world! lisi');
//        $cache->delete('name');
//        $cache->flush();
        $cache->add('name','hello world',15);
        $cache->set('username','hello world aa',15);


    }
    public function actionCacheget()
    {
        $cache = Yii::$app->cache;
        echo $cache->get('username');
    }

    public function actionUsertag()
    {
        $usertag = new UsersTag();
        $usertag->tag_title = '特聘培训员';
        $usertag->save();
        print_r($usertag->getErrors());
    }
}